package com.igor.reporting.pages.issue;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import com.igor.reporting.dao.contactPerson.ContactPersonDAO;
import com.igor.reporting.dao.issue.IssueDAO;
import com.igor.reporting.dao.location.LocationDAO;
import com.igor.reporting.dao.problem.ProblemDAO;
import com.igor.reporting.dao.solver.SolverDAO;
import com.igor.reporting.entities.contactPerson.ContactPerson;
import com.igor.reporting.entities.issue.Issue;
import com.igor.reporting.entities.location.Location;
import com.igor.reporting.entities.problem.Problem;
import com.igor.reporting.entities.solver.Solver;
import com.igor.reporting.model.GenericSelectModel;

public class Add {

	@Inject
	private PropertyAccess access;
	@Inject
	private IssueDAO issueDAO;
	
	@Property
	@Persist
	private Issue issue;
	
	@Property
	@Persist
	private Location location;
	private GenericSelectModel<Location> locations;
	@Inject
	private LocationDAO locationDAO;
	
	public GenericSelectModel<Location> getLocations() {
		locations = new GenericSelectModel<Location>(
				locationDAO.findAll(),
				Location.class, "name", "id", access);
		return locations;
	}
	
	@Property
	@Persist
	private ContactPerson contactPerson;
	private GenericSelectModel<ContactPerson> contactPersons;
	@Inject
	private ContactPersonDAO contactPersonDAO;
	
	public GenericSelectModel<ContactPerson> getContactPersons() {
		contactPersons = new GenericSelectModel<ContactPerson>(
				contactPersonDAO.findAll(),
				ContactPerson.class, "name", "id", access);
		return contactPersons;
	}
	
	
	@Property
	@Persist
	private Problem problem;
	private GenericSelectModel<Problem> problems;
	@Inject
	private ProblemDAO problemDAO;
	
	public GenericSelectModel<Problem> getProblems() {
		problems = new GenericSelectModel<Problem>(
				problemDAO.findAll(),
				Problem.class, "category", "id", access);
		return problems;
	}
	
	@Property
	@Persist
	private Solver solver;
	private GenericSelectModel<Solver> solvers;
	@Inject
	private SolverDAO solverDAO;
	
	public GenericSelectModel<Solver> getSolvers() {
		solvers = new GenericSelectModel<Solver>(
				solverDAO.findAll(),
				Solver.class, "name", "id", access);
		return solvers;
	}
	
	Object onSuccess() {
		issue.setContactPerson(contactPerson);
		issue.setLocation(location);
		issue.setProblem(problem);
		issue.setSolver(solver);
		issueDAO.add(issue);
		return com.igor.reporting.pages.issue.Browse.class;
	}
	
}
